<template>
  <view class="login-container">
    <!-- 头部 -->
    <view class="header">
      <text>用户登录</text>
    </view>
    
    <!-- 内容区域 -->
    <view class="content">
      <!-- 登录表单 -->
      <view class="form-group">
        <label for="username">用户名</label>
        <view class="input-wrapper">
          <i class="fas fa-user"></i>
          <input 
			v-model="loginForm.username"
            type="text" 
            id="username" 
            placeholder="请输入用户名" 
          >
        </view>
      </view>
      
      <view class="form-group">
        <label for="password">密码</label>
        <view class="input-wrapper">
          <i class="fas fa-lock"></i>
          <input 
			v-model="loginForm.password"
            type="password"
			 :password = "showpasswordflag"
			 @click="showpassword"
            id="password" 
            placeholder="请输入密码" 
            
          >
        </view>
      </view>
      
      <button class="btn-login" @click="handleLogin">
        <span v-if="!loading">登录</span>
        <view v-else class="loading-spinner"></view>
      </button>
      
     <view class="divider">
        <span>还没有账号？</span>
      </view>
      
     <view class="social-login">
		<view class="social-btn alipay" @click="toregin">
          <text class="fab fa-alipay">注册</text>
        </view>
      </view>
      
    <!-- 页脚 -->
    <view class="footer">
		<view>
			<text>博客管理系统  <a href="#">© 2025</a></text>
			<br/>
      <text> 隐私政策 | 服务条款</text>
		</view>
      
    </view>
    
    <!-- 使用请求组件 -->
    <RequestComponent 
      ref="requestComponent"
      :url="baseurl+'/user/login'"
      method="POST"
      :request-data="loginForm"
    />
  </view>
  </view>
</template>

<script>
import RequestComponent from '@/component/RequestCom.vue';
import myquest from "@/untils/request.js"
export default {
  components: {
    RequestComponent
  },
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loading: false,
      requestResult: null,    //接收请求结果
	  showpasswordflag:true,
	  baseurl:myquest.baseurl
    };
  },
  methods: {
    // 处理登录逻辑
    handleLogin() {       
      if (!this.loginForm.username || !this.loginForm.password) {   //其中有一个为空
        this.requestResult = {       //填写的返回结果
          success: false,
          message: '请输入用户名和密码',
          timestamp: new Date().toISOString()     //时间
        };
		uni.showToast({
			title:"请输入用户名和密码",
			icon:'error'
		})
        return;
      }
      
	  //都填写了
      this.loading = true;      //正在加载
      this.requestResult = null;    // 没有返回结果
      // 使用请求组件发送登录请求
      const requestComponent = this.$refs.requestComponent;    //获取组件
      
      requestComponent.sendRequest()    //发送登录请求
        .then(response => {
          this.requestResult = {
            success: true,
            status: response.statusCode,
            data: response.data,       //返回的数据
            timestamp: new Date().toISOString()
          };
          this.loading = false;
          
          // 在实际项目中，这里会处理登录成功后的逻辑
          // 例如保存token、跳转页面等
		  if(response.statusCode===200){
			  if(response.data.code === 0){
				  let token = response.data.data   //获取到token
				  //持久化到本地
				  uni.setStorage({
				  	key:"token",
					data:token
				  })
				  this.$store.commit("SET_TOKEN",token)   //保存全局
				  // this.$store.commit("SET_ISLOGIN",true)  
				  myquest.autologin().then(r=>{
					  this.$store.commit("SET_USER",r.user.data.data)  //全局保存用户
					  this.$store.commit("SET_TOKEN",r.token)  
					  this.$store.commit("SET_ISLOGIN",true)  
					  uni.$emit("login")
				  })
				  //返回信息
				  this.requestResult = {
				    success: false,
				    status: response.data.code,
				    message: response.data.message,
				    timestamp: new Date().toISOString()
				  };
				  uni.showToast({
				  	title:"登录成功",
					icon:'success'
				  })
				  uni.switchTab({
				  	url:"/pages/index/index"
				  })
				  uni.$emit("loginok")
				  
			  }else{    //密码不对或者 
				  uni.showToast({
					title:response.data.message,
					icon:'error'
				  })
				 this.requestResult = {
				   success: false,
				   status: response.data.code,
				   message: response.data.message,
				   timestamp: new Date().toISOString()
				 };
			  }
		  } 
		})
        .catch(error => {
			uni.showToast({
				title: '网络问题或者服务器暂未开放',
				icon:'error'
			})
          this.requestResult = {
            success: false,
            status: error.statusCode || 500,
            message: error.errMsg || '网络请求失败',
            timestamp: new Date().toISOString()
          };
          this.loading = false;
        });
    },
    
    // 格式化请求结果显示
    formatResult(result) {
      if (result.success) {
        return JSON.stringify(result.data, null, 2);
      } else {
        return `错误代码: ${result.status || 500}\n错误信息: ${result.message || '未知错误'}`;
      }
    },
	toregin(){
		uni.navigateTo({
			url:"/pages/router/regin/regin"
		})
	}, 
	showpassword(){
		this.showpasswordflag = !this.showpasswordflag
	},
	// registersuccess(user){
	// 	console.log("login -- 213");
	// 	console.log(this);
	// 	console.log(this.loginForm);
	// 	this.loginForm.username = user.username
	// 	this.loginForm.password = user.password
	// }
  },
  //在加载完成时候，进行自动登录
  onLoad(option) {
	// uni.$on("registersuccess",this.registersuccess)
  },
  onUnload() {
  	// uni.$off("registersuccess")
  }
};
</script>

<style scoped>
/* * {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'PingFang SC', 'Helvetica Neue', Arial, sans-serif;
} */

.login-container {
  width: 100%;
  height: 100vh;
  max-width: 500px;
  margin: 0 auto;
  background: white;
  border-radius: 10px;
  box-shadow: 0 15px 50px rgba(0, 0, 0, 0.2);
  overflow: hidden;
}

.header {
	display: flex;
	justify-content: center;
	align-items: center;
	height: 46px;
  background: linear-gradient(to right, #4776E6, #8E54E9);
  color: white;
  padding: 12px 8px;
  text-align: center;
}

.header text {
  font-size: 25px;
  font-weight: 600;
  margin-bottom: 8px;
}


.content {
  padding: 30px 30px 10px 30px;
}

.form-group {
  margin-bottom: 25px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: #333;
  font-size: 15px;
}

.input-wrapper {
  position: relative;
}

.input-wrapper i {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #777;
  font-size: 18px;
}

.input-wrapper input {
  width: 80%;
  padding: 10px 10px 10px 40px;
  border: 2px solid #e1e5f0;
  border-radius: 12px;
  font-size: 16px;
  transition: all 0.3s;
  outline: none;
}

.input-wrapper input:focus {
  border-color: #4776E6;
  box-shadow: 0 0 0 3px rgba(71, 118, 230, 0.2);
}

.btn-login {
  background: linear-gradient(to right, #4776E6, #8E54E9);
  color: white;
  border: none;
  border-radius: 12px;
  padding: 5px;
  font-size: 18px;
  font-weight: 400;
  width: 80%;
  cursor: pointer;
  transition: all 0.3s;
  box-shadow: 0 4px 15px rgba(71, 118, 230, 0.3);
}

.btn-login:hover {
  transform: translateY(-4px);
  box-shadow: 0 7px 20px rgba(71, 118, 230, 0.4);
}

.btn-login:active {
  transform: translateY(2px) scale(0.95);
}

.divider {
  display: flex;
  align-items: center;
  margin: 25px 0;
}

.divider span {
  padding: 0 15px;
  color: #777;
  font-size: 14px;
}

.divider::before,
.divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: #e1e5f0;
}

.social-login {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-bottom: 25px;
}

.social-btn {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: white;
  cursor: pointer;
  transition: all 0.3s;
}

.social-btn:hover {
  transform: translateY(-3px) scale(0.95);
}

.social-btn.wechat {
  background: linear-gradient(to right, #09BB07, #2ADD1C);
}

.social-btn.alipay {
 background: linear-gradient(to right, #1677FF, #1E90FF);
    /* background: linear-gradient(to right, #4776E6, #8E54E9); */
	font-size: 1em;
}

.social-btn.apple {
  background: linear-gradient(to right, #000, #333);
}

.footer {
	width: 90%;
	position: relative;
	bottom: 0;
	display: flex; 
	justify-content: center; 
	align-items: center;
  text-align: center;
  padding: 0 20px 20px 20px ;
  color: #777;
  font-size: 14px;
  border-top: 1px solid #eee;
}
.footer > view{
	/* width: 80%; */
	/* background-color: #4776E6; */
	text-align: center;
}

.footer a {
  color: #4776E6;
  text-decoration: none;
  font-weight: 500;
}

.result-container {
  background: #f8f9ff;
  border-radius: 12px;
  padding: 20px;
  margin-top: 25px;
  border: 1px solid #e1e5f0;
  max-height: 300px;
  overflow-y: auto;
}

.result-title {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 15px;
  color: #4776E6;
  display: flex;
  align-items: center;
  gap: 10px;
}

.result-content {
  font-family: 'Courier New', monospace;
  font-size: 14px;
  line-height: 1.6;
  color: #333;
  white-space: pre-wrap;
  word-break: break-all;
}

.loading-spinner {
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-top: 3px solid white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.success-badge {
  display: inline-flex;
  align-items: center;
  background: #e6f7ee;
  color: #09bb07;
  padding: 5px 12px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 500;
}

.error-badge {
  display: inline-flex;
  align-items: center;
  background: #fdeeee;
  color: #f44336;
  padding: 5px 12px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 500;
}
</style>